package com.quyeying.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyeying.annotation.PassToken;
import com.quyeying.entity.TblMerchantEntity;
import com.quyeying.entity.TblPopularizeVideoEntity;
import com.quyeying.entity.TblProductEntity;
import com.quyeying.service.TblFollowMerchantService;
import com.quyeying.service.TblMerchantService;
import com.quyeying.service.TblPopularizeVideoService;
import com.quyeying.service.TblProductService;
import com.quyeying.utils.TokenUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* tbl_merchant 前端控制器
* </p>
* @author skx
* @since 2022-12-12
*/
@Slf4j
@RestController
@RequestMapping("merchantEntity")
public class TblMerchantController {

    @Resource
    private TblMerchantService tblMerchantService;
    @Resource
    TblProductService productService;
    @Resource
    TblFollowMerchantService followMerchantService;
    @Resource
    TblPopularizeVideoService popularizeVideoService;

    /**
     * 获取商户主页信息
     * @return
     */
    @PassToken
    @GetMapping("/getMerchantInfo")
    public R getIndexInfo(HttpServletRequest request, Long merchantId) {
        //最新发布视频
        QueryWrapper<TblPopularizeVideoEntity> popularizeVideoEntityQueryWrapper = new QueryWrapper<>();
        popularizeVideoEntityQueryWrapper.select("id as videoId, video, pid").eq("merchant_id", merchantId).last("ORDER BY sort desc, create_time desc limit 1");
        Map<String, Object> map = new HashMap<>(4);
        map.put("video",popularizeVideoService.getMap(popularizeVideoEntityQueryWrapper));
        Long popularity = tblMerchantService.getPopularity(merchantId);
        map.put("popularity", popularity);
        //商品总浏览量
        QueryWrapper<TblProductEntity> productEntityQueryWrapper = new QueryWrapper<>();
        productEntityQueryWrapper.select("COALESCE(sum(product_browse),0) as productBrowse").eq("merchant_id", merchantId);
        TblProductEntity productBrowse = productService.getOne(productEntityQueryWrapper);
        map.put("productBrowse", productBrowse.getProductBrowse());
        //发布者信息
        TblMerchantEntity merchant = tblMerchantService.getByMerchantId(merchantId);
        // 是否关注商户
        merchant.setAttentionStatus(TokenUtil.verfiyToken(request) ? followMerchantService.isFollow(TokenUtil.getUserId(request), merchantId) : false);
        map.put("merchantInfo", merchant);
        return R.success(map);
    }



    /**
     * 商户更多（搜索）接口
     **/
    @PassToken
    @GetMapping("/findListPageSearch")
    public R findListPageSearch(@RequestParam Integer current, @RequestParam Integer size, @RequestParam String search) {

        try {
            return tblMerchantService.findListPageSearch(current, size, search);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }
}
